from lxml import etree

xml_str = """
<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
</bookstore>
"""




tree = etree.XML(xml_str)
print(dir( tree))


book = tree.find('book')
print(book.attrib)

books = tree.findall('book')
for book in books:
    print(book.attrib)

title = tree.find('book/title')
year= tree.find('book/year')
print(title.text, year.text)

tree = etree.fromstring(xml_str,parser=etree.HTMLParser())
print(type(tree))

html_str = """
<html>
<body>
<h1 id="title">This is a Heading</h1>
<p class="intro">This is a paragraph.</p>
</body>
</html>
"""
tree = etree.HTML(html_str)
print(type(tree))
print(tree.tag,tree.attrib)
print(tree.text)
print(tree.getroottree())

h = tree.find('body/h1')
p = tree.findall('body/p')
print(h.tag, h.attrib)

print(h.text)
for p0 in p:
    print(p0.tag, p0.attrib)